//==============================================================================
//
// Title:       BIST_CheckCssaAndFpgaPowSeq.c
// Purpose:     A short description of the implementation.
//
// Created on:  2013-3-1 at 15:00:35 by Vinson.Zhang
// Copyright:   dafda. All Rights Reserved.
//
//==============================================================================

#include "RFBB.h"         
#include <ansi_c.h>


_FRMON_TEST_STRUCT_CMD g_cFRMONCssaFpgaPowSeqCmd[5] = {{"FRMon,action,load,agent,VegaAccess,Vegas,_vegaAccess","done"},
                                                        {"FRMon/Vegas/_vegaAccess,action,init,VegaAccess,","done"},
                                                        {"FRMon/Vegas/_vegaAccess,action,writeRegister,0x0023,0x001","0x00"},
														{"FRMon/Vegas/_vegaAccess,action,readRegister,0x0023","0x00"},
                                                        {"FRMon/Vegas/_vegaAccess,action,terminate,VegaAccess","done"}};

/**************************************************************************************************
  Function:        BIST_CheckCssaAndFpgaPowSeq
  Description:    This test aims to check that everything went fine in the power sequencing of ASICs and FPGAs and in the boot up of MCU
  Calls:           none
  Called By:       dll
  Table Accessed:  none
  Table Updated:   none
  Input:           none
  Output:          error code
  Return:          none
  Test Case ID: CSSA FPGA Power Sequencing Alarm Test
  Used Command:    g_cFRMONCssaFpgaPowSeqCmd
**************************************************************************************************/
void __decldllexp BIST_CheckCssaAndFpgaPowSeq(tTestData *testData, tTestError *testError)
{ 
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};

    char* pszLookupStr = NULL;
    char  szElementPath[BUFFER_SIZE_256] = {'\0'};
    char  szExpectResult[BUFFER_SIZE_32] = {'\0'}; 
    int error = 0;
    int iPassFail = 0;
	int iArryNum = 0;
	
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));
   
    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );
   
    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));
    
	//InetTelnetOpen(FRCommand)
	g_hFrmonAgent = RfbbInetTelnetOpen (g_szDUTIP, g_iPort, 0);          

    for(iArryNum = 0;iArryNum < 4;iArryNum++)
    	{
        sprintf(kszShmonAgent,g_cFRMONCssaFpgaPowSeqCmd[iArryNum].cWriteFrmonCmd);    
      	FRmonAgent_String(kszShmonAgent, -1,g_cFRMONCssaFpgaPowSeqCmd[iArryNum].cReturnValue, &iPassFail);
	
        if (iPassFail!=1) 
        	{
            goto Error;
        	}
		
		Delay(1);
    	} 
	
Error: 
	  //action terminate
    sprintf(kszShmonAgent,g_cFRMONCssaFpgaPowSeqCmd[4].cWriteFrmonCmd);
    Frmon_RunCmdNoAck(kszShmonAgent, 1);

#if(ON_OFF != SIMU_VERS)

    if(iPassFail == PASS) 
    {
        testData->result = PASS;      
    }
    else 
    {
        testData->result = FAIL;
    }    

    //InetTelnetClose(FRCommand)
	InetTelnetClose(g_hFrmonAgent);
	
	//FREE RESOURCES 
    CA_DISCARD_OBJ(hStepName);
    CA_FREE(pszStepName);

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if (error < 0)
    {
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    }
#else
    testData->result = PASS;
#endif
    // Clear message from GUI
    TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

    return;    
}


// ************************* 78 character ruler *****************************
//                           End of source file
// *************************************

